package io.renren.modules.back.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.back.entity.CategoryEntity;
import io.renren.modules.back.entity.GoodsAttributeEntity;
import io.renren.modules.back.entity.GoodsEntity;
import io.renren.modules.back.entity.GoodsGalleryEntity;
import io.renren.modules.back.service.CategoryService;
import io.renren.modules.back.service.GoodsAttributeService;
import io.renren.modules.back.service.GoodsGalleryService;
import io.renren.modules.back.service.GoodsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品接口
 *
 * @author zzh
 * @email 862583291@qq.com
 * @date 2019年2月11日 上午10:40:10
 */
@RestController
@RequestMapping("/back/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private GoodsAttributeService goodsAttributeService;

    @Autowired
    private GoodsGalleryService goodsGalleryService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("goods:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        String categoryId = (String) params.get("categoryId");
        if( categoryId != null &&  !("".equals(categoryId)) ) {
            CategoryEntity categoryEntity = categoryService.getById(Integer.parseInt(categoryId));
            if( categoryEntity.getParentId() == 0 ){
                List<CategoryEntity> categoryEntityList = categoryService.list(
                        new QueryWrapper<CategoryEntity>().eq("parent_id",categoryId).eq("is_show",1)
                );
                List<Integer> childCategoryIds = new ArrayList<>();
                for (CategoryEntity category : categoryEntityList) {
                    childCategoryIds.add(category.getId());
                }
                query.remove("categoryId");
                query.put("categoryIds", childCategoryIds);
            }
        }
        query.put("isDelete", 0);
        List<GoodsEntity> goodsList = goodsService.queryList(query);
        int total = goodsService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(goodsList, total, query.getLimit(), query.getCurrPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("goods:info")
    public R info(@PathVariable("id") Integer id) {
        GoodsEntity goods = goodsService.getById(id);
        List<GoodsAttributeEntity> attributeEntityList = goodsAttributeService.list(
                new QueryWrapper<GoodsAttributeEntity>().eq("goods_id",id).orderByAsc("attribute_id")
        );
        List<GoodsGalleryEntity> goodsImgList = goodsGalleryService.list(
                new QueryWrapper<GoodsGalleryEntity>().eq("goods_id",id).orderByAsc("sort_order")
        );
        goods.setAttributeEntityList(attributeEntityList);
        goods.setGoodsImgList(goodsImgList);
        return R.ok().put("data", goods);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goods:save")
    public R save(@RequestBody GoodsEntity goods) {
        int id = goodsService.saveGoods(goods);
        return R.ok().put("id",id);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("goods:update")
    public R update(@RequestBody GoodsEntity goods) {
        goodsService.update(goods);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goods:delete")
    public R delete(@RequestBody Integer[] ids) {
        goodsService.deleteBatch(ids);
        return R.ok();
    }

}
